package cn.bluech.template;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.StringRes;


/**
 * Description:
 * Create by Codeant on 2018/12/9 0009
 */
public class App extends Application {
    private static App instance;
    private static Toast toast;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    private static void _toast(String info) {
        if (null == instance)
            return;
        if(null==toast){
            toast = Toast.makeText(instance,info,Toast.LENGTH_SHORT);
        }else{
            toast.setText(info);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    /**
     * 简易消息提示框
     * @param info 弹出的消息
     */
    public static void toast(final String info){
        if(null==instance)
            return;
        // Toast需要有looper才能显示，否是会抛异常
        if(Looper.myLooper()!=Looper.getMainLooper()){
            new Handler(Looper.getMainLooper()).post(() -> _toast(info));
        }else
            _toast(info);
    }

    public static void toast(@StringRes int strId){
        if(null!=instance){
            toast(instance.getString(strId));
        }
    }
}
